//
//  SanLouController.m
//  民族文化宫
//
//  Created by WANG CUNRUI on 14-6-7.
//  Copyright (c) 2014年 WANG CUNRUI. All rights reserved.
//

#import "SanLouController.h"
#import "MZcondetailViewController.h"
#import "FPPopoverController.h"
#import "KxMenu.h"
#import "SoundTool.h"

@interface SanLouController ()
{
    AVAudioPlayer   *_player;
    UIBackgroundTaskIdentifier  _bgTaskId;
}
@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UIPopoverController *popover;
@property CGFloat lastRotation;
@property CGRect frame;
@property (strong, nonatomic) UIButton *button1;
@property (strong, nonatomic) UIButton *button2;
@property (strong, nonatomic) UIButton *button3;
@property (strong, nonatomic) UIButton *button4;
@property (strong, nonatomic) UIButton *button5;
@property (strong, nonatomic) NSString *s1;
@property (strong, nonatomic) NSString *s2;
@property (strong, nonatomic) NSString *s3;
@property (strong, nonatomic) NSString *s4;
@property (strong, nonatomic) NSString *p;
@property (assign, nonatomic) BOOL  enableMusicPlay;
@property (assign, nonatomic) BOOL  isclick;
@end

@implementation SanLouController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _bgTaskId = [SoundTool backgroundPlayerID:_bgTaskId];
        _player = [SoundTool audioPlayerWithName:@"jieshao.mp3"];
        [_player setNumberOfLoops:-1];
        [_player prepareToPlay];
    }
    return self;
}

- (void)viewDidLoad
    {
        [super viewDidLoad];
        self.edgesForExtendedLayout = UIRectEdgeNone;
        [self.view setBackgroundColor:[UIColor redColor]];
        
            CGRect frame = self.view.bounds;
            CGSize f = frame.size;
            CGFloat h = f.height;
            CGFloat w = f.width;
            self.imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"113.jpg"]];
            self.imageView.frame  = CGRectMake(0, 0, w, h-110);
            [self.view addSubview:self.imageView];
        _button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        _button1.frame = CGRectMake(82, 124, 25, 25);
        [_button1 setBackgroundColor:[UIColor clearColor]];
        [_button1 addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];
        _button1.tag = 1;
        [self.view addSubview:_button1];
        _button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        _button2.frame = CGRectMake(78, 257, 25, 25);
        [_button2 setBackgroundColor:[UIColor clearColor]];
        [_button2 addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];
        _button2.tag = 2;
        [self.view addSubview:_button2];
        _button3 = [UIButton buttonWithType:UIButtonTypeCustom];
        _button3.frame = CGRectMake(213, 105, 25, 25);
        [_button3 setBackgroundColor:[UIColor clearColor]];
        [_button3 addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];
        _button3.tag = 3;
        [self.view addSubview:_button3];
        _button4 = [UIButton buttonWithType:UIButtonTypeCustom];
        _button4.frame = CGRectMake(249, 225, 25, 25);
        [_button4 setBackgroundColor:[UIColor clearColor]];
        [_button4 addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];
        _button4.tag = 4;
        [self.view  addSubview:_button4];
        _button5 = [UIButton buttonWithType:UIButtonTypeCustom];
        _button5.frame = CGRectMake(278, 404, 40, 40);
        [_button5 setBackgroundColor:[UIColor clearColor]];
        [_button5 addTarget:self action:@selector(butClick) forControlEvents:UIControlEventTouchUpInside];
        _button5.tag = 5;
        [self.view  addSubview:_button5];
        UIImageView *lightImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2.png"]];
        lightImage.frame = CGRectMake(270,397,30,30);
        [self.view  addSubview:lightImage];
        
        _s1 = [NSString stringWithFormat:@"古代艺术陶瓷展"];
        _s2 = [NSString stringWithFormat:@"历代珍藏文物展"];
        _s3 = [NSString stringWithFormat:@"明清书画艺术展"];
        _s4 = [NSString stringWithFormat:@"多功能展厅"];

    }

- (void)showMenu:(UIButton *)sender
{
    _p = [[NSString alloc]init];
    if (sender.tag == 1) {
        _p = _s1;
    }
    else if (sender.tag ==2)
    {
        _p = _s2;
    }
    else if (sender.tag ==3)
    {
        _p = _s3;
    }
    else if (sender.tag ==4)
    {
        _p = _s4;
    }
    NSArray *menuItems =
    @[
      
      [KxMenuItem menuItem:_p
                     image:nil
                    target:nil
                    action:NULL],
      
      [KxMenuItem menuItem:@"进入详情"
                     image:[UIImage imageNamed:@"action_icon"]
                    target:self
                    action:@selector(pushMenuItem:)],
      
      [KxMenuItem menuItem:@"播放导游音频"
                     image:[UIImage imageNamed:@"check_icon"]
                    target:self
                    action:@selector(soundplayer)],
      [KxMenuItem menuItem:@"停止导游音频"
                     image:[UIImage imageNamed:@"check_icon"]
                    target:self
                    action:@selector(stopplayer)],
      ];
    
    KxMenuItem *first = menuItems[0];
    first.foreColor = [UIColor colorWithRed:47/255.0f green:112/255.0f blue:225/255.0f alpha:1.0];
    first.alignment = NSTextAlignmentCenter;
    
    [KxMenu showMenuInView:self.view
                  fromRect:sender.frame
                 menuItems:menuItems];
}

- (void) pushMenuItem:(id)sender
{
    _PP = [[MZZLdetailViewController alloc]init];
    _PP.title1 = _p;
    [self.navigationController pushViewController:_PP animated:YES];
}

- (void) soundplayer
{

    [_player play];
    _isclick = NO;
    [self butClick];
    

}

- (void) stopplayer
{
    [_player stop];
    _isclick = YES;
    [self butClick];
}

- (void)butClick
{
    
    if (_isclick == NO) {
        UIImageView *lightImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
        lightImage.frame = CGRectMake(270,397,30,30);
        [self.view addSubview:lightImage];
        _bgTaskId = [SoundTool backgroundPlayerID:_bgTaskId];
        _player = [SoundTool audioPlayerWithName:@"上01一东.mp3"];
        [_player setNumberOfLoops:-1];
        [_player prepareToPlay];
        [_player play];
        _isclick =YES;

    }else
    {
        UIImageView *lightImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2.png"]];
        lightImage.frame = CGRectMake(270,397,30,30);
        [self.view addSubview:lightImage];
        _isclick = NO;
        [_player stop];
        
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
